//
//  NSString+MMDWrapper.m
//  MeMeDaiSteward
//
//  Created by Mime97 on 16/1/26.
//  Copyright © 2016年 Mime. All rights reserved.
//

#import "NSString+MMDWrapper.h"

@implementation NSString (MMDWrapper)

- (BOOL)equals:(NSString *)anotherString
{
    return [self isEqualToString:anotherString];
}

- (int)checkLoginPWD
{
    int returnValue = 0;
    
    NSString * string = [self trim];
    
    //判断字符长度！
    if (string.length < 8 || string.length > 16) {
        returnValue = 1;
        return returnValue;
    }
    
    const char *c = [string UTF8String];
    
    if (![self isNumberAndCharCompelted:c]) {
        
        return 2;
    }
    
    if (![self isComplexCompelted:c]) {
        
        return 3;
    }
    return returnValue;
}

- (BOOL)isNumberAndCharCompelted:(const char *)testCharList{
    
    int i = 0;
    char testC = testCharList[i];
    
    BOOL isAllNumber = YES;
    BOOL isAllChar   = YES;
    BOOL isSpecial   = NO;
    
    
    while (testC != '\0') {
        
        if (testC <= '9' && testC >= '0'){
            
            isAllChar = NO;
        }else if ((testC >= 'A' && testC <= 'Z') || (testC >= 'a' && testC <= 'z')){
            
            isAllNumber = NO;
        }else{
            
            isSpecial = YES;
            return NO;
            break;
        }
        
        testC = testCharList[++i];
    }
    
    if (isAllNumber || isAllChar) {
        
        return NO;
    }
    
    return YES;
}

- (BOOL)isComplexCompelted:(const char *)testCharList{
    
    int i = 1;
    
    int maxNumberOfDisqualification = 0;
    int upNumbers = 1;
    int downNumbers = 1;
    int equalNumbers = 1;
    
    char nomalChar = testCharList[1];
    char frontChar = testCharList[0];
    
    BOOL isAllEqual = YES;
    
    
    while (nomalChar != '\0') {
        
        if (equalNumbers >= 6) {
            
            return NO;
            break;
        }
        
        if (nomalChar != frontChar) {
            
            equalNumbers = 1;
            isAllEqual = NO;
            
            if (nomalChar == frontChar + 1) {
                
                if (maxNumberOfDisqualification < downNumbers) {
                    
                    maxNumberOfDisqualification = downNumbers;
                }
                downNumbers = 1;
                upNumbers ++;
                
                if (maxNumberOfDisqualification >= 6) {
                    return NO;
                }
                
            }else if (nomalChar == frontChar - 1){
                
                if (maxNumberOfDisqualification < upNumbers) {
                    
                    maxNumberOfDisqualification = upNumbers;
                }
                upNumbers = 1;
                downNumbers ++;
                
                if (maxNumberOfDisqualification >= 6) {
                    return NO;
                }
                
                
            }else{
                
                
                
                if (upNumbers < downNumbers) {
                    
                    upNumbers = downNumbers;
                }
                
                if (maxNumberOfDisqualification < upNumbers) {
                    
                    maxNumberOfDisqualification = upNumbers;
                }
                
                if (maxNumberOfDisqualification >= 6) {
                    return NO;
                }
                
                downNumbers = 1;
                upNumbers   = 1;
            }
            
        }else{
            
            
            if (upNumbers < downNumbers) {
                
                upNumbers = downNumbers;
            }
            
            if (maxNumberOfDisqualification < upNumbers) {
                
                maxNumberOfDisqualification = upNumbers;
            }
            
            
            if (maxNumberOfDisqualification >= 6) {
                return NO;
            }
            
            downNumbers = 1;
            upNumbers   = 1;
            equalNumbers += 1;
        }
        
        
        frontChar = nomalChar;
        nomalChar = testCharList[++i];
    }
    
    
    return YES;
}

- (BOOL)isNullOrEmpty {
    
    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (self == nil || self == NULL || [self isEqualToString:@""]) {
        return YES;
    }
    if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    }
    if ([self isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}

- (NSString *) trim {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSArray *) split:(NSString*) separator {
    return [self componentsSeparatedByString:separator];
}
@end
